<template>
  <input type="file" @change="getFile($event)" multiple="multiple">
</template>


<script>
import axios from 'axios'
export default {
  data() {
    return {
      files: [],
    }
  },
  methods: {
    getFile: function (event) {
      this.files = event.target.files;
      if(this.files.length >=2){
            this.files = [];
            alert('只能选择一张图片作为课程封面');
      }
    },
    submit: function (event) {
      //阻止元素发生默认的行为 
      event.preventDefault();
      let formData = new FormData();
      for (let i = 0; i < this.files.length; i++) {
        formData.append("files", this.files[i]);
      }
      // 请求路径
      var w = '/file/yp/upload';
      let config = {  
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      axios.post(w, formData, config)
        .then( (response)=> {
          console.log("上传成功");
          console.log(response.data)
          this.src = response.data;
        })
        .catch(function (error) {
          alert("图片上传失败");
          console.log(error);
        });
    }
  }
}
</script>